package com.example.administrator.jokeyboarddemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.view.View;

public class FractionView extends View {
    private String number;
    private String molecle; //分子
    private String denominator;//分母
    
    private Canvas mCanvas;
    
    private Paint mPaint;
	public FractionView(Context context) {
		this(context,null);
		
	}
	public FractionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
	}
	
	  // 绘制图片
			 @Override
			protected void onDraw(Canvas canvas) {
			   super.onDraw(canvas);
			   mPaint =new Paint(Paint.ANTI_ALIAS_FLAG);
			   mPaint.setColor(Color.BLACK);
			   mPaint.setStrokeWidth(2);
			   Bitmap mBitmap = createBitmapByText(mPaint,"999999999999","32","99914",30);
			   canvas.drawBitmap(mBitmap,0,0,null);	
			   
			}
			 
    
	public Bitmap createBitmapByText(Paint mPaint, String number, String molecle, String denominator,int textSize) {
		mPaint.setTextSize(textSize);
		
		int trueWidth = 0;
		int trueHeight = 0;
		
		int width = 0;
		int height = 0; 
		
		FontMetrics fm = mPaint.getFontMetrics();  
		float textHeight  = (float) Math.ceil(fm.descent - fm.ascent); 
	
		int numberWidth = 0;
		int molecleWidth = (int) mPaint.measureText(molecle);
		int denominatorWidth = (int) mPaint.measureText(denominator);
		int bigTextWidth = 0;
		
	 	//计算图片的宽高
		 height = (int) (textHeight*2+4*4);
	     bigTextWidth =  molecleWidth >= denominatorWidth?molecleWidth:denominatorWidth;
	     width = (int) (bigTextWidth / 3.0f * 5);
		if(number != null &&!"".equals(number)) {
		   numberWidth = (int) (mPaint.measureText(number)+5*2);//数字所占宽度
	       width = width+numberWidth;
		}
		float textWidth =0;
		
		//创建一个bitmap
		Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		
		//mCanvase会把信息绘制到bitmap 上面 
		Canvas mCanvas = new Canvas(bitmap);
		mCanvas.drawARGB(255, 255, 0, 0); 
	
		if(number == null ||"".equals(number)) {
	     trueWidth =  width;
		}else {
		 trueWidth = width - numberWidth;
		 
		 // 绘制前方数字
		  textWidth = mPaint.measureText(number);
		  float x = numberWidth/2.0f-textWidth/2.0f;
		  float y = height/2.0f - (mPaint.descent()+mPaint.ascent())/2.0f;
		  mCanvas.drawText(number,x ,y, mPaint);
			
		}
		 //绘制分子
		  textWidth = mPaint.measureText(molecle);
		  float x = trueWidth/2.0f-textWidth/2.0f+numberWidth;
		  float y = height/4.0f - (mPaint.descent()+mPaint.ascent())/2.0f ;
		  mCanvas.drawText(molecle,x ,y, mPaint);
		  
	      //绘制中线
		  mCanvas.drawLine(numberWidth+5, height/2,width-5, height/2,mPaint);
		  
	       //绘制分母
		   textWidth = mPaint.measureText(denominator);
		   x = trueWidth/2-textWidth/2+numberWidth;
		   y = height/4.0f - (mPaint.descent()+mPaint.ascent())/2.0f+height/2;
		   mCanvas.drawText(denominator, x, y, mPaint);
		
		   return bitmap;
		
	}
}
